import {ignoreProps} from './const'; import {SubscriptionObject} from './subscription-object'; import {Alert} from 'react-native'; export class MasterObject { propsSubscriptions: {[id: string]: SubscriptionObject[]} = {}; constructor(private masterValue: any) { Object.keys(masterValue).forEach( (key) => (this.propsSubscriptions[key] = []), ); } destroySubscription = (subscriptionObject: SubscriptionObject) => { Object.keys(this.masterValue).forEach((key) => { if (this.propsSubscriptions[key]) this.propsSubscriptions[key].filter((so) => subscriptionObject === so); }); }; subscribeToProp = ( prop: string | number | symbol, subscriptionObject: SubscriptionObject, ) => { if (!this.propsSubscriptions[prop as string]) this.propsSubscriptions[prop as string] = []; this.propsSubscriptions[prop as string].push(subscriptionObject); }; createSubscription() { return new SubscriptionObject( this.masterValue, this.subscribeToProp, this.destroySubscription, ); } updateValues = (newMasterValue: any) => { const subcriptionsObjectsForUpdate: SubscriptionObject[] = []; Object.keys(newMasterValue).forEach((key) => { if (newMasterValue[key] !== this.masterValue[key]) { if (this.propsSubscriptions[key]) this.propsSubscriptions[key].forEach((subscriptionObject) => { if (!subscriptionObject.readyForUpdate) { subscriptionObject.value[key] = newMasterValue[key]; subcriptionsObjectsForUpdate.push(subscriptionObject); } subscriptionObject.readyForUpdate = true; }); } }); subcriptionsObjectsForUpdate.forEach((so) => so.update()); this.masterValue = newMasterValue; }; }